package com.template.app.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.template.app.exception.ObjectNotFoundException;
import com.template.app.model.Template;
import com.template.app.model.TemplateRef;
import com.template.app.service.TemplateService;
import com.template.app.support.ControllerHelper;

@Controller
@RequestMapping(Template.NAME)
public class TemplateController {

	public static final String BASE_PATH = "/" + Template.NAME;
	public static final String VIEW_LIST = Template.NAME + ControllerHelper.VIEW_LIST;
	public static final String VIEW_FORM = Template.NAME + ControllerHelper.VIEW_FORM;

	@Autowired
	private TemplateService service;
	
	@RequestMapping(value = ControllerHelper.NEW, method = RequestMethod.GET)
	public ModelAndView neww(ModelMap model) {
		ModelAndView mav = ControllerHelper.getModelAndView(VIEW_FORM, new Template());
		addTemplateRefs(mav);
		return mav;
	}
	
	@RequestMapping(value = ControllerHelper.EDIT, method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mav = ControllerHelper.getModelAndView(VIEW_FORM, service.getFetch(id));
		addTemplateRefs(mav);
		return mav;
	}
	
	@RequestMapping(value = ControllerHelper.ADD, method = RequestMethod.POST)
	public ModelAndView add(@Valid Template t, BindingResult result) {
		t.setId(null);
		return saveOrUpdate(t, result);
	}
	
	@RequestMapping(value = ControllerHelper.UPDATE, method = RequestMethod.PUT)
	public ModelAndView update(@PathVariable Integer id, @Valid Template t, BindingResult result) {
		t.setId(id);
		return saveOrUpdate(t, result);
	}
	
	@RequestMapping(value = ControllerHelper.DELETE, method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		try {
			service.delete(id);
		} catch (ObjectNotFoundException e) {
			//TODO
		}
		return ControllerHelper.redirect(BASE_PATH);
	}
	
	@RequestMapping(value = ControllerHelper.GET, method = RequestMethod.GET)
	public ModelAndView get(@PathVariable Integer id) {			
		Template obj = service.get(id);
		return new ModelAndView(VIEW_FORM, Template.NAME, obj);
	}
	
	@RequestMapping(value = ControllerHelper.LIST, method = RequestMethod.GET)
	public ModelAndView list() {		
		List<Template> list = service.list();
		return new ModelAndView(VIEW_LIST, ControllerHelper.LIST_PARAM, list);
	}
	
	@RequestMapping(value = ControllerHelper.LIST_BY + TemplateRef.NAME, method = RequestMethod.GET)
	public ModelAndView lisByTemplateRef(@RequestParam Integer id) {		
		List<Template> list = service.lisByTemplateRef(id);
		return new ModelAndView(VIEW_LIST, ControllerHelper.LIST_PARAM, list);
	}

	private ModelAndView saveOrUpdate(Template t, BindingResult result) {
		if (result.hasErrors()) {			
			return ControllerHelper.handleValidationError(VIEW_FORM, result);
		} else {
			service.save(t);
		}
		
		System.out.println("locale: " + LocaleContextHolder.getLocale().toString());
		
//		return ControllerHelper.redirect(BASE_PATH);
		return ControllerHelper.getSuccess(VIEW_LIST);
	}
	
	private void addTemplateRefs(ModelAndView mav) {
		TemplateRef r1 = new TemplateRef();
		r1.setId(1);
		r1.setName("REF 1");

		TemplateRef r2 = new TemplateRef();
		r2.setId(2);
		r2.setName("REF 2");
		
		List<TemplateRef> list = new ArrayList<TemplateRef>();
		list.add(r1);
		list.add(r2);
		
		mav.addObject("templateRefs", list);
	}
}
